package kr.co.handysoft.smartPolice.server;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.zip.DataFormatException;

import kr.co.handysoft.common.compress.Archiving;
import kr.co.handysoft.common.registry.SmartPoliceRegistry;
import kr.co.handysoft.project.smartPolice.composite.SystemStatusComposite;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.SWTResourceManager;

public class Worker implements Runnable {

	private String data;
	private String zone;
	private String kor;
	private TreeItem folderItem;

	public Worker(String data, String zone, String kor, TreeItem folderItem) {
		this.data = data;
		this.zone = zone;
		this.kor = kor;
		this.folderItem = folderItem;
	}

	@Override
	public void run() {
		String recvString;
		
		try {
			recvString = new String(Archiving.decompress(Archiving.hexToByteArray(data)));
			JSONObject recvJSONObj = JSONObject.fromObject(recvString);
			String commandString = recvJSONObj.getString("command");
			
			if (commandString.equals("exit")){
				
				this.folderItem.getParentItem().dispose();
				
			}else if (commandString.equals("files")){
			
				JSONArray jsonArray = recvJSONObj.getJSONArray("files");
				int arraySize = jsonArray.size();
				final HashMap<String, Long> siteNameMap = new HashMap<String, Long>();
				for (int i=0; i<arraySize; i++){
					
					JSONObject jobj = jsonArray.getJSONObject(i);
					final String siteName = jobj.getString("site");
					String fileName = jobj.getString("date") + "_" + jobj.getString("site") + "_" + jobj.getString("line") + "_" + jobj.getString("number") + "_" + jobj.getString("speed");
					String fDreamSavePath = SmartPoliceRegistry.get4DreamSavePath();
					File file = new File(fDreamSavePath + fileName.replaceAll("\\.jpg", "") + ".txt");
					Writer out = new OutputStreamWriter(new FileOutputStream(file));
					String formatString = String.format("\n\n%s\n%s\n\n\n\n%s\n\n\n\n\n\n\n\n\n", this.zone, this.kor, jobj.getString("ftpPath"));
					out.write(formatString);
					out.flush();
					out.close(); 
					
					Object siteObject = siteNameMap.get(siteName);
					if (siteObject == null) siteNameMap.put(siteName, new Long(1L));
					else siteNameMap.put(siteName, ((Long)siteObject) + 1L);
				}
					
				if (this.folderItem != null && !folderItem.isDisposed()){
					
					this.folderItem.getDisplay().asyncExec(new Runnable() {
						
						@Override
						public void run() {
							Set<String> keySet = siteNameMap.keySet();
							Iterator<String> keyIterator = keySet.iterator();
							while(keyIterator.hasNext()){
								
								String siteName = (String)keyIterator.next();									
								Long siteCntLong = siteNameMap.get(siteName);
								Object siteObject = folderItem.getData(siteName);
								NumberFormat nf = NumberFormat.getNumberInstance();
								
								if (siteObject == null){
									TreeItem treeItem_1 = new TreeItem(folderItem, 0);
									treeItem_1.setText(siteName + " - " + nf.format(siteCntLong));
									treeItem_1.setImage(SWTResourceManager.getImage(SystemStatusComposite.class, "/com/famfamfam/slik/icons/camera.png"));
									treeItem_1.setData("cnt", siteCntLong);
									folderItem.setData(siteName, treeItem_1);
								}else{
									TreeItem treeItem_1 = (TreeItem)siteObject;
									Long longVal = (Long)treeItem_1.getData("cnt");
									longVal += siteCntLong;
									treeItem_1.setText(siteName + " - " + nf.format(longVal));
									treeItem_1.setData("cnt", longVal);
								}

								Object cntLong = folderItem.getData("cnt");
								Object nameString = folderItem.getData("name");
								
								if (cntLong == null){
									folderItem.setData("cnt", siteCntLong);
									folderItem.setText(nameString + " - " + nf.format(siteCntLong));
								}else{
									Long longVal = (Long)cntLong;
									longVal += siteCntLong;
									folderItem.setData("cnt", longVal);
									folderItem.setText(nameString + " - " + nf.format(longVal));
								}
																
								TreeItem zoneItem = folderItem.getParentItem();
								Object zoneItemLong = zoneItem.getData("cnt");
								Object namezoneItemString = zoneItem.getData("name");
								
								if (zoneItemLong == null){
									zoneItem.setData("cnt", new Long(1));
									zoneItem.setText(namezoneItemString + " - " + nf.format(siteCntLong));
								}else{
									Long longVal = (Long)zoneItemLong;
									longVal += siteCntLong;
									zoneItem.setData("cnt", longVal);
									zoneItem.setText(namezoneItemString + " - " + nf.format(longVal));
								}
								
								TreeItem areaItem = zoneItem.getParentItem();
								Object areaItemLong = areaItem.getData("cnt");
								Object nameareaItemString = areaItem.getData("name");
								
								if (areaItemLong == null){
									areaItem.setData("cnt", new Long(1));
									areaItem.setText(nameareaItemString + " - " + nf.format(siteCntLong));
								}else{
									Long longVal = (Long)areaItemLong;
									longVal += siteCntLong;
									areaItem.setData("cnt", longVal);
									areaItem.setText(nameareaItemString + " - " + nf.format(longVal));
								}
							}
						}
					});
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DataFormatException e) {
			e.printStackTrace();
		}
		
	}

}
